# 这是一个示例 Python 脚本。
from datetime import datetime

import numpy as np
# 按 Shift+F10 执行或将其替换为您的代码。
# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。
import pandas as pd
import warnings

warnings.filterwarnings("ignore")


def print_hi(name):
    # 在下面的代码行中使用断点来调试脚本。
    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。


def cal(s1, s2):
    s1 = s1[:-4]
    s2 = s2[:-4]
    # print(s1, s2)
    d1 = datetime.strptime(s1, "%H:%M:%S")
    d2 = datetime.strptime(s2, "%H:%M:%S")
    # print((int((d2 - d1).seconds / 3)) > 1)
    return (int((d2 - d1).seconds / 3)) > 1


def select(path):
    df = pd.read_csv(path)
    str1 = '10:00:00:000'
    str2 = '11:00:00'
    index = -1
    df1 = pd.DataFrame(columns=df.columns)
    cnt = 0
    df1 = pd.concat([df1, df[0:1]])
    for i in range(1, len(df)):
        # if i % 20 == 0:
        df1 = pd.concat([df1, df[i:i + 1]])
        cnt = cnt + 1
        if cal(df.iloc[i - 1]['UpdateTime'], df.iloc[i]['UpdateTime']):
            print(i)
            df1 = pd.concat([df1, df[i:i + 1]])
            cnt = cnt + 1
            index = i
    # print(index)
    return df1


def get_chazhi():
    df = pd.read_csv('csv/data_raw_11_month_stand_date.csv')
    print(df.columns)
    print(df.head())
    df['Vol'] = -1
    for i in range(1, len(df)):
        if i % 201 == 0:
            # print(i-1, df.loc[i-1, 'UpdateTime'])
            # print(i, df.loc[i, 'UpdateTime'])
            # print(i+1, df.loc[i+1, 'UpdateTime'])
            # df.loc[i, 'Vol'] = df.loc[i, 'OT'] - df.loc[i - 1, 'OT']
            continue
        df.loc[i, 'Vol'] = df.loc[i, 'Volume'] - df.loc[i - 1, 'Volume']
        # df.loc[i, 'Vol'] = df.loc[i,'OT'] - df.loc[i-1, 'OT']
    # df1 = pd.DataFrame(columns=df.columns)
    # cnt = 0
    # # df1 = pd.concat([df1, df[0:1]])
    # df1 = df1.drop(index=df1[(df1.Vol <= 0.0)].index.tolist())
    df.to_csv('data_raw_11_month_stand_date_1.csv')


def softmax(data):
    new_data = data * 1.0 / np.sum(data)
    # print(new_data)
    return new_data


def step():
    # pass
    # step1
    # df = pd.read_csv('data_raw_11_month_stand_date.csv')
    # print(df.head())
    # print(len(df))
    # print(len(df) / 201)
    # # step2
    # get_chazhi()
    # step3
    # df = pd.read_csv('data_raw_11_month_stand_date_1.csv')
    # print(df.head())
    # print(len(df))
    # print(len(df) / 201)
    # for i in range(0, len(df)):
    #     if i % 201 == 0:
    #         print(df.loc[i, 'UpdateTime'])
    # df1 = df.drop(index=df[(df.Vol == -1)].index.tolist())
    # df1.to_csv('data_raw_11_month_stand_date_2.csv')
    # step4
    df = pd.read_csv('csv/data_raw_11_month_stand_date_2.csv')
    print(df.head())
    print(len(df))
    print(len(df) / 200)
    vol = df['Vol'].to_numpy()
    print(len(vol))
    vol_1 = np.zeros(vol.shape)
    for i in range(0, 239):
        vol_1[i * 200:(i + 1) * 200] = softmax(vol[i * 200:(i + 1) * 200])
    print(vol_1[-100:])
    print(len(vol_1))
    df['OT'] = vol_1
    print(len(df) / 200)
    print(df.head())
    df.to_csv('data_raw_11_month_stand_date_3.csv')


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    features = ['AskPrice1', 'AskPrice2', 'AskPrice3', 'AskPrice4', 'AskPrice5',
                'AskPrice6', 'AskPrice7', 'AskPrice8', 'AskPrice9', 'AskPrice10', 'AskVolume1',
                'AskVolume2', 'AskVolume3', 'AskVolume4', 'AskVolume5',
                'AskVolume6', 'AskVolume7', 'AskVolume8', 'AskVolume9', 'AskVolume10', 'BidPrice1',
                'BidPrice2', 'BidPrice3',
                'BidPrice4', 'BidPrice5', 'BidPrice6',
                'BidPrice7', 'BidPrice8', 'BidPrice9', 'BidPrice10', 'BidVolume1',
                'BidVolume2', 'BidVolume3', 'BidVolume4', 'BidVolume5', 'BidVolume6',
                'BidVolume7', 'BidVolume8', 'BidVolume9', 'BidVolume10', 'Volume']

    # step()
    # step 5
    # df = pd.read_csv('data_raw_11_month_stand_date_3.csv')
    # print(df.head()['UpdateTime'])
    # new_data = df.loc[:, ~df.columns.str.contains("^Unnamed")]
    # print(new_data.head())
    # del new_data['Volume']
    # del new_data['Vol']
    # new_data.to_csv('data_raw_11_month_stand_date_4.csv', index=False)
    # step 6
    # df = pd.read_csv('data_raw_11_month_stand_date_4.csv')
    # print(df.head())
    # time = df['UpdateTime'].tolist()
    # print(time)
    # df['date'] = [v[:-4] for v in time]
    # print(df.head())
    # del df['UpdateTime']
    # df.to_csv('data_raw_11_month_stand_date_5.csv', index=False)

    # step 7
    # df = pd.read_csv('data_raw_11_month_stand_date_5.csv')
    # # df['date'] = str(df['date'])[str(df['date']).index('2'):]
    # date = df['date'].tolist()
    # d = []
    # cnt = 0
    # for v in date:
    #     index =v.index('2021')
    #     print(v[index:])
    #     d.append(v[index:])
    #     cnt +=1
    # print(cnt)
    # df['date']=d
    # print(df.head(17002))
    # #
    # df['date'] = pd.to_datetime(df['date'], format="%Y-%m-%d %H:%M:%S")
    # print(df.head())
    # df.to_csv('data_raw_11_month_stand_date_6.csv', index=False)

    # df = pd.read_csv('mid.csv')
    # print(df.head(100))
    # df = df.loc[:, ~df.columns.str.contains("^Unnamed")]
    # date = df['UpdateTime'].tolist()
    #
    # d = []
    # cnt = 0
    # for v in date:
    #     index = v.index('2021')
    #     # print(v[index:])
    #     d.append(v[index:-4])
    #     cnt += 1
    # print(cnt)
    # df['date'] = d
    # print(df.head())
    # df.to_csv('mid_1.csv', index=False)
    df = pd.read_csv('mid_1.csv')
    df['MidPrice'] = (df['AskPrice1'] + df['BidPrice1']) / 2
    print(df.head())
    print(df.columns.tolist())
    # df['date'] = pd.to_datetime(df['date'], format="%Y-%m-%d %H:%M:%S")
    del df['UpdateTime']
    df.to_csv('mid_3.csv', index=False)
    print(df.head())
    # df["date"] = pd.to_datetime(df['UpdateTime'], errors='coerce')
    # print(df.head())
    # get_chazhi()
    # df = pd.read_csv('data_raw_6_month_stand.csv')
    # # df1 = df.drop(index=df[(df.Vol == -1)].index.tolist())
    # # df1.to_csv('mic_data_raw.csv')
    # #df['UpdateTime'] = df['UpdateTime'][:-4]
    # print(df.head(100))
    # print(len(df))
    # index = df[(df.BidVolume10 >= 0)].index.tolist()
    # get_chazhi()
    # df = pd.read_csv('data_raw_6_month_stand_1.csv')
    # print(df.head())
    # # print(index)
    # #get_chazhi()
    #
    # df1 = df.drop(index=df[(df.Vol == -1)].index.tolist())
    # print(df1[:1202].head())
    # print(df1[:1202].tail())
    # df = pd.read_csv('data_raw_6_month_stand_1.csv')
    # print(df.head())
    # # get_chazhi()
    # print(len(df) / 201)
    # print(len(df))
    # for i in range(0, len(df)):
    #     if i % 201 == 0:
    #         print(df.loc[i, 'Vol'])
    # df1 = df.drop(index=df[(df.Vol == -1)].index.tolist())
    # df1.to_csv('data_raw_6_month_stand_2.csv')

    # for i in range(0, len(df)):
    #     if i % 1200 == 0:
    #         # print(i - 1, df.loc[i - 1, 'UpdateTime'], df.loc[i-1, 'Vol'])
    #         print(i, df.loc[i, 'UpdateTime'], df.loc[i, 'Vol'])
    #         # print(i + 1, df.loc[i + 1, 'UpdateTime'], df.loc[i+1, 'Vol'])

    # df_all = pd.DataFrame(columns=features)
    # print(df_all.head())
    # df = pd.read_csv('2021-1-4.csv')
    # # df = df[features]
    # df['OT'] = df['Volume']
    # df = df.drop(labels=['Volume'], axis=1)
    # columns = df.columns.tolist()
    # print(columns)
    # print(np.sort(np.asarray(columns)))
    # df_all = pd.concat([df_all, df])
    # path = 'data\\2022\\2022-12\\2022-12-6.csv'
    # path1 = '2022-12-6.csv'
    # df = select(path)
    # print(df)
    # df = pd.read_csv(path)
    # for i in range(0, len(df)):
    #     if i % 1 == 0:
    #         print(i, df.iloc[i]['UpdateTime'], df.iloc[i]['AskPrice1'])
    # df.to_csv(path1)

# data\2021\2021-12\2021-12-26.csv 1200
# data\2022\2022-11\2022-11-22.csv 1200
# data\2022\2022-12\2022-12-26.csv 1200
# data\2022\2022-12\2022-12-27.csv 1200
# data\2022\2022-12\2022-12-6.csv 1155
# data\2022\2022-4\2022-4-21.csv 1200
# data\2022\2022-9\2022-9-27.csv 1200
# data\2022\2022-9\2022-9-8.csv 1200

# 1200 00:00.0
# 1201 00:00.0
# 1202 00:03.0
